/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.CreatureDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.Creature;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Creature} and
 * {@link cz.muni.fi.pa165.creatures.dto.CreatureDTO} and vice versa.
 *
 * @author smikloso
 */
public class CreatureMapping implements MappingDTO<CreatureDTO, Creature> {

    @Autowired
    private RegionMapping regionMapping;

    @Autowired
    private CreatureTypeMapping creatureTypeMapping;

    @Override
    public CreatureDTO entityToDTO(Creature creature) {
        if (creature == null) {
            return null;
        }

        CreatureDTO dto = new CreatureDTO();
        dto.setId(Long.toString(creature.getId()));

        if (creature.getName() != null) {
            dto.setName(creature.getName());
        }
        if (creature.getWeight() != null) {
            dto.setWeight(creature.getWeight());
        }
        if (creature.getHeight() != null) {
            dto.setHeight(creature.getHeight());
        }
        if (creature.getActualHitPoints() != null) {
            dto.setActualHitPoints(creature.getActualHitPoints());
        }
        if (creature.getRegion() != null
                && creature.getRegion().getName().length() > 0) {
            dto.setRegionDTO(regionMapping.entityToDTO(creature.getRegion()));
        }
        if (creature.getCreatureType() != null
                && creature.getCreatureType().getName().length() > 0) {
            dto.setCreatureTypeDTO(creatureTypeMapping.entityToDTO(creature.getCreatureType()));
        }

        System.out.println(dto.toString());
        
        return dto;
    }

    @Override
    public Creature DTOtoEntity(CreatureDTO dto) {
        Creature creature = new Creature();
        if (dto == null) {
            return null;
        }
        if (dto.getId() != null) {
            creature.setId(Long.parseLong(dto.getId()));
        }
        if (dto.getName() != null) {
            creature.setName(dto.getName());
        }
        if (dto.getHeight() != null) {
            creature.setHeight(dto.getHeight());
        }
        if (dto.getWeight() != null) {
            creature.setWeight(dto.getWeight());
        }
        if (dto.getActualHitPoints() != null) {
            creature.setActualHitPoints(dto.getActualHitPoints());
        }
        if (dto.getRegionDTO() != null) {
            creature.setRegion(regionMapping.DTOtoEntity(dto.getRegionDTO()));
        }
        if (dto.getCreatureTypeDTO() != null) {
            creature.setCreatureType(creatureTypeMapping.DTOtoEntity(dto.getCreatureTypeDTO()));
        }

        return creature;
    }
}
